package com.edu.service.impl;

import com.edu.bean.Book;
import com.edu.bean.BookExample;
import com.edu.mapper.BookMapper;
import com.edu.service.BookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:何包蛋
 * @Date :2022/11/10 - 11 - 10 - 15:36
 */
@Service
@Transactional
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;

    @Override
    public Book getById(int bookId) {
        return bookMapper.selectByPrimaryKey(bookId);
    }

    @Override
    public void delBook(int writerId) {
        BookExample bookExample = new BookExample();
        bookExample.createCriteria().andWriteridEqualTo(writerId);
        List<Book> books = bookMapper.selectByExample(bookExample);
        if (books.size() != 0){
            int i = bookMapper.delByWriterid(writerId);
            if (i == 1) {
                System.out.println("该作者写的书 删除成功！！！");
            }else {
                System.out.println("该作者写的书 删除失败！！！");
            }
        }else{
            System.out.println("未查询到有该作者写的书！！！");
        }
    }

    @Override
    public List<Book> getAll(String keyWord) {
        List<Book> books = bookMapper.selectAll(keyWord);
        return books;
    }

    @Override
    public List<Book> getAllBook() {
        return bookMapper.selectAllBook();
    }
}
